Imports Microsoft.VisualBasic
Imports ClubStarterKit.Data
Imports SubSonic
Imports System.Web.HttpContext
Imports System.Linq
Imports System.Data.Linq

Namespace ClubStarterKit.Web
    Public Class Settings

        Public Shared Sub PushToSession()
            If Current.Session.Item("SettingsSet") Is Nothing Or CBool(Current.Session.Item("SettingsSet")) = False Then
                Dim ws As New WebSettings(WebSettings.Columns.Id, 1)

                If ws.IsLoaded = True Then
                    Current.Session.Item(WebSettings.Columns.Name) = ws.Name
                    Current.Session.Item(WebSettings.Columns.Slogan) = ws.Slogan
                    Current.Session.Item(WebSettings.Columns.Copyright) = ws.Copyright
                    Current.Session.Item(WebSettings.Columns.Address) = ws.Address
                    Current.Session.Item(WebSettings.Columns.SiteURL) = ws.SiteURL
                    Current.Session.Item(WebSettings.Columns.MailServer) = ws.MailServer
                    Current.Session.Item(WebSettings.Columns.MailUsername) = ws.MailUsername
                    Current.Session.Item(WebSettings.Columns.EmailVerification) = ws.EmailVerification
                    Current.Session.Item(WebSettings.Columns.AdminVerification) = ws.AdminVerification
                    Current.Session.Item(WebSettings.Columns.PageSize) = ws.PageSize
                    Current.Session.Item(WebSettings.Columns.ReportCMS) = ws.ReportCMS
                    Current.Session.Item(WebSettings.Columns.Webmaster) = ws.Webmaster
                    Current.Session.Item(WebSettings.Columns.IsHighestWinner) = ws.IsHighestWinner
                    Current.Session.Item(WebSettings.Columns.ModerationLevel) = ws.ModerationLevel
                    'Current.Session.Item(WebSettings.Columns.CurrentSeason) = ws.CurrentSeason
                    Current.Session.Item("SettingsSet") = True
                Else
                    WebSettings.Insert("My Club Name", "My Club Slogan or Tag Line", "Copyright &copy 2007 My Club. All Rights Reserved.", "My Club's Address", "http://localhost/", "mail.myclub.com", "club@myclub.com", "mailpassword", False, False, 5, False, "webmaster@myclub.com", True, 0, 0)
                    Dim ws2 As New WebSettings(WebSettings.Columns.Name, "My Club Name")
                    Current.Session.Item(WebSettings.Columns.Name) = ws2.Name
                    Current.Session.Item(WebSettings.Columns.Slogan) = ws2.Slogan
                    Current.Session.Item(WebSettings.Columns.Copyright) = ws2.Copyright
                    Current.Session.Item(WebSettings.Columns.Address) = ws2.Address
                    Current.Session.Item(WebSettings.Columns.SiteURL) = ws2.SiteURL
                    Current.Session.Item(WebSettings.Columns.MailServer) = ws2.MailServer
                    Current.Session.Item(WebSettings.Columns.MailUsername) = ws2.MailUsername
                    Current.Session.Item(WebSettings.Columns.EmailVerification) = ws2.EmailVerification
                    Current.Session.Item(WebSettings.Columns.AdminVerification) = ws2.AdminVerification
                    Current.Session.Item(WebSettings.Columns.PageSize) = ws2.PageSize
                    Current.Session.Item(WebSettings.Columns.ReportCMS) = ws2.ReportCMS
                    Current.Session.Item(WebSettings.Columns.Webmaster) = ws2.Webmaster
                    Current.Session.Item(WebSettings.Columns.IsHighestWinner) = ws2.IsHighestWinner
                    Current.Session.Item(WebSettings.Columns.ModerationLevel) = ws2.ModerationLevel
                    'Current.Session.Item(WebSettings.Columns.CurrentSeason) = ws2.CurrentSeason
                    Current.Session.Item("SettingsSet") = True
                End If

                
                'Current.Session.Item(WebSettings.Columns.CurrentSeason)
            End If
        End Sub

        Public Shared Function Name() As String
            If Current.Session.Item(WebSettings.Columns.Name) Is Nothing Then
                PushToSession()
                Dim ws As New WebSettings(WebSettings.Columns.Id, 1)
                Return ws.Name
            Else
                Return Current.Session.Item(WebSettings.Columns.Name).ToString()
            End If
        End Function

        Public Shared Function Slogan() As String
            If Current.Session.Item(WebSettings.Columns.Slogan) Is Nothing Then
                PushToSession()
                Dim ws As New WebSettings(WebSettings.Columns.Id, 1)
                Return ws.Slogan
            Else
                Return Current.Session.Item(WebSettings.Columns.Slogan).ToString()
            End If
        End Function

        Public Shared Function Copyright() As String
            If Current.Session.Item(WebSettings.Columns.Copyright) Is Nothing Then
                PushToSession()
                Dim ws As New WebSettings(WebSettings.Columns.Id, 1)
                Return ws.Copyright
            Else
                Return Current.Session.Item(WebSettings.Columns.Copyright).ToString()
            End If
        End Function

        Public Shared Function Address() As String
            If Current.Session.Item(WebSettings.Columns.Address) Is Nothing Then
                PushToSession()
                Dim ws As New WebSettings(WebSettings.Columns.Id, 1)
                Return ws.Address
            Else
                Return Current.Session.Item(WebSettings.Columns.Address).ToString()
            End If
        End Function

        Public Shared Function SiteURL() As String
            If Current.Session.Item(WebSettings.Columns.SiteURL) Is Nothing Then
                PushToSession()
                Dim ws As New WebSettings(WebSettings.Columns.Id, 1)
                Return ws.SiteURL
            Else
                Return Current.Session.Item(WebSettings.Columns.SiteURL).ToString()
            End If
        End Function

        Public Shared Function MailServer() As String
            If Current.Session.Item(WebSettings.Columns.MailServer) Is Nothing Then
                PushToSession()
                Dim ws As New WebSettings(WebSettings.Columns.Id, 1)
                Return ws.MailServer
            Else
                Return Current.Session.Item(WebSettings.Columns.MailServer).ToString()
            End If
        End Function

        Public Shared Function MailUsername() As String
            If Current.Session.Item(WebSettings.Columns.MailUsername) Is Nothing Then
                PushToSession()
                Dim ws As New WebSettings(WebSettings.Columns.Id, 1)
                Return ws.MailUsername
            Else
                Return Current.Session.Item(WebSettings.Columns.MailUsername).ToString()
            End If
        End Function

        Public Shared Function MailPassword() As String
            Dim ws As New WebSettings(WebSettings.Columns.Id, 1)
            Return ws.MailPassword
        End Function

        Public Shared Function EmailVerification() As Boolean
            If Current.Session.Item(WebSettings.Columns.EmailVerification) Is Nothing Then
                PushToSession()
                Dim ws As New WebSettings(WebSettings.Columns.Id, 1)
                Return ws.EmailVerification
            Else
                Return CBool(Current.Session.Item(WebSettings.Columns.EmailVerification).ToString())
            End If
        End Function

        Public Shared Function AdminVerification() As Boolean
            If Current.Session.Item(WebSettings.Columns.AdminVerification) Is Nothing Then
                PushToSession()
                Dim ws As New WebSettings(WebSettings.Columns.Id, 1)
                Return ws.AdminVerification
            Else
                Return CBool(Current.Session.Item(WebSettings.Columns.AdminVerification).ToString())
            End If
        End Function

        Public Shared Function PageSize() As Integer
            If Current.Session.Item(WebSettings.Columns.PageSize) Is Nothing Then
                PushToSession()
                Dim ws As New WebSettings(WebSettings.Columns.Id, 1)
                Return ws.PageSize
            Else
                Return CInt(Current.Session.Item(WebSettings.Columns.PageSize).ToString())
            End If
        End Function

        Public Shared Function ReportCMS() As Boolean
            If Current.Session.Item(WebSettings.Columns.ReportCMS) Is Nothing Then
                PushToSession()
                Dim ws As New WebSettings(WebSettings.Columns.Id, 1)
                Return ws.ReportCMS
            Else
                Return CBool(Current.Session.Item(WebSettings.Columns.ReportCMS).ToString())
            End If
        End Function

        Public Shared Function Webmaster() As String
            If Current.Session.Item(WebSettings.Columns.Webmaster) Is Nothing Then
                PushToSession()
                Dim ws As New WebSettings(WebSettings.Columns.Id, 1)
                Return ws.Webmaster
            Else
                Return Current.Session.Item(WebSettings.Columns.Webmaster).ToString()
            End If
        End Function

        Public Shared Function IsHighestWinner() As Boolean
            If Current.Session.Item(WebSettings.Columns.IsHighestWinner) Is Nothing Then
                PushToSession()
                Dim ws As New WebSettings(WebSettings.Columns.Id, 1)
                Return ws.IsHighestWinner
            Else
                Return CBool(Current.Session.Item(WebSettings.Columns.IsHighestWinner).ToString())
            End If
        End Function

        Public Shared Function StoreImagesOnFile() As Boolean
            If ConfigurationManager.AppSettings("StoreImagesOnFile").ToString() = "True" Then
                Return True
            Else
                Return False
            End If
        End Function

        Public Shared Function ModerationLevel() As Integer
            If Current.Session.Item(WebSettings.Columns.ModerationLevel) Is Nothing Then
                PushToSession()
                Dim ws As New WebSettings(WebSettings.Columns.Id, 1)
                Return ws.ModerationLevel
            Else
                Return CInt(Current.Session.Item(WebSettings.Columns.ModerationLevel).ToString())
            End If
        End Function

        Public Shared Function CurrentSeason() As Integer
            If Current.Session.Item(WebSettings.Columns.CurrentSeason) Is Nothing Then
                PushToSession()
                
                Using dbContext As New CricketBLL.CricketStatsDataContext(Settings.DatabaseConnectionString)
                    Dim season = From s In dbContext.Seasons _
                                 Where s.SeasonStart < DateTime.Now AndAlso s.SeasonEnd > DateTime.Now _
                                 Select s
                    If season.Count = 1 Then
                        Dim s = season.Single()
                        Current.Session.Item(WebSettings.Columns.CurrentSeason) = s.SeasonId
                    End If

                End Using

                Return CInt(Current.Session.Item(WebSettings.Columns.CurrentSeason).ToString())
            Else
                Return CInt(Current.Session.Item(WebSettings.Columns.CurrentSeason).ToString())
            End If
        End Function

        <System.ComponentModel.DataObjectMethodAttribute _
        (System.ComponentModel.DataObjectMethodType.Select, False)> _
        Public Shared Function LoadSetting() As WebSettings
            Dim a As New WebSettings(WebSettings.Columns.Id, 1)
            Return a
        End Function

        <System.ComponentModel.DataObjectMethodAttribute _
        (System.ComponentModel.DataObjectMethodType.Update, False)> _
        Public Shared Sub SaveSetting(ByVal stngs As WebSettings)
            WebSettings.Update(1, stngs.Name, stngs.Slogan, stngs.Copyright, stngs.Address, stngs.SiteURL, stngs.MailServer, stngs.MailUsername, stngs.MailPassword, stngs.EmailVerification, stngs.AdminVerification, stngs.PageSize, stngs.ReportCMS, stngs.Webmaster, stngs.IsHighestWinner, stngs.ModerationLevel, stngs.CurrentSeason)
        End Sub


        Public Shared ReadOnly Property DatabaseConnectionString() As String
            Get
                Return System.Web.Configuration.WebConfigurationManager.ConnectionStrings("ClubDBConnectionString").ConnectionString
            End Get
        End Property


    End Class
End Namespace